/**
 * @file toperrors.h
 * @brief Macros of the top-errors functions.
 * @date Dec-2011
 * @author Leonardo Lino 2101101
 * @author Joaquim Ferreira 2101104
 * @version 1.0
 */
 
 #ifndef TOPERRORS_H
#define TOPERRORS_H
 
 /** @brief Structure which holds sinonim list and the number of times this word has tested positive. Used in a hashtable */
 typedef struct
{
  LISTA_GENERICA_T* sinonims; /** @brief sinonims for missing word */
  int count; /** @brief Word counter */
} STRTOPERR;


/**
 * @brief Main processor for top-errors funcionality
 * @fn void mainTopErrors(char *foldername, char *dictionary, int numThreads)
 * @param foldername A pointer to a folder which wil be crawled
 * @param dictionary A pointer to the dictionary string.
 * @param numThreads Number of threads to be created
 */
void mainTopErrors(char *foldername, char *dictionary, int numThreads);

/**
 * @brief Processes all word elements in a generic list and places the result in a hashtable
 * @fn HASHTABLE_T* processEachWordTopError(LISTA_GENERICA_T* fileHolder,HASHTABLE_T* dictHolder)
 * @param fileHolder Generic list containing processed words from an individual txt file
 * @param dictHolder Pointer to a hashtable containing specified dictionary
 * @return Hashtable is returned which holds the results. Said results are held in a structure
 */
HASHTABLE_T* processEachWordTopError(LISTA_GENERICA_T* fileHolder,HASHTABLE_T* dictHolder);

/**
 * @brief Check if specified word is already in the hashtable
 * @fn checkHash(HASHTABLE_T* localHash, LISTA_GENERICA_T* resultsList, char *word)
 * @param localHash Hashtable to be comparared against
 * @param resultsList List of sinonims which were a positive match for specific word
 * @param word Word is used to compare againt hashtable keys
 */
void checkHash(HASHTABLE_T* localHash, LISTA_GENERICA_T* resultsList, char *word);

/**
 * @brief Inserts sinonims from one list into another. Checks to see if sinonim already exists in destination list.
 * @fn void insertSinonims(LISTA_GENERICA_T* origin, LISTA_GENERICA_T* destination)
 * @param origin Initial list
 * @param destination Initial list will be copied into this list
 */
void insertSinonims(LISTA_GENERICA_T* origin, LISTA_GENERICA_T* destination);

/**
 * @brief Merges 2 hashes together.
 * @fn void mergeHashes(HASHTABLE_T* origin, HASHTABLE_T* destination)
 * @param origin Initial hasttable
 * @param destination Initial hasttable will be copied into this hasttable and counters properly sumed
 */
void mergeHashes(HASHTABLE_T* origin, HASHTABLE_T* destination);

/**
 * @brief Prnts to screen a hashtable
 * @fn void outputTopHash(HASHTABLE_T* currentHash)
 * @param currentHash Hashtable that will be output
 */
void outputTopHash(HASHTABLE_T* currentHash);

/**
 * @brief Functionality is similar to checkHash() with the diference that it will properly merge STRTOPERR structures
 * @fn void checkHashFinal(STRTOPERR *jn, char* item, HASHTABLE_T* destination)
 * @param currentHash Hashtable that will be output
 */
void checkHashFinal(STRTOPERR *jn, char* item, HASHTABLE_T* destination);



/**
 * @brief used to compare to generic list items by name. Used by lista_pesquisar() which is a listas.c specific function.
 * @fn int compareForLists(char* a1, char* a2)
 * @param a1 First word to be compared.
 * @param a2 second word to be compared. Both parameters are pointers.
 * @return returns either -1,0 or 1 depending on whether a1 is less than, equal or greater than a2
 */
int compareForLists(char* a1, char* a2);

/**
 * @brief Deprecated function. Processes all txt files in a folder and subfolders. 
 * @fn LISTA_GENERICA_T* folderProcessor(char *foldername )
 * @param foldername A pointer to a folder which wil be crawled
 * @return Return a generic list containing all txt filenames with their full path
 */
LISTA_GENERICA_T* folderProcessor(char *foldername );


/**
 * @brief Free memory used by finalResults hashtable
 * @fn void freeFinalResults(HASHTABLE_T* finalResults)
 * @param finalResults hashtable to be released
 */
void freeFinalResults(HASHTABLE_T* finalResults);

#endif /* TOPERRORS_H */
